package com.sun.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Arrays;


/**
 * @author SunRui
 * @description 切面类
 * @date 2024/9/26 14:59
 */
@Component
@Aspect
public class AopAspect {

    private String NAME = "AopAspect";

    private static final Logger logger = Logger.getLogger(AopAspect.class.getName());

//    @Pointcut("execution(int com.sun.service.impl.BookServiceImpl.addBook(..))")
    @Pointcut("execution(* com.sun.service.impl.*.*(..))")
    private void pt(){}

    /*
    进入
     */
    @Before("pt()")
    public void Before(){
        logger.debug(NAME+"-开始时间:"+System.currentTimeMillis());
    }


    /*
    解析
     */
    @Before("pt()")
    public void BeforeAnalysis(JoinPoint jp){
        logger.debug("当前调用的类:"+jp.getTarget()+"\t调用的方法:"+jp.getSignature().getName());
        logger.debug("传递的参数:"+ Arrays.asList(jp.getArgs()));
    }

    /*
    结束
     */
    @After("pt()")
    public void After(){
        logger.debug(NAME+"-结束时间:"+System.currentTimeMillis());
    }

    /*
    环绕
     */
    @Around("pt()")
    public Object Around(ProceedingJoinPoint pj) throws Throwable {
        logger.debug("Around-开始");
        Object proceed = null;
        try {
            proceed = pj.proceed();
        } catch (Exception e) {
            logger.debug("出现错误:" + e.getMessage());
        }
        logger.debug("Around-结束");
        return proceed;
    }



}
